
package cn.ydt.ydt.user.webapi.service;

import cn.ydt.ydt.common.exception.YdtServiceException;
import cn.ydt.ydt.common.pojo.domain.UserJwtVO;
import cn.ydt.ydt.common.utils.FileNameUtils;
import cn.ydt.ydt.common.utils.IDCardsUtils;
import cn.ydt.ydt.common.utils.PasswordUtils;
import cn.ydt.ydt.pojo.user.dto.UserRegisterDTO;
import cn.ydt.ydt.pojo.user.dto.UserUpdateDTO;
import cn.ydt.ydt.pojo.user.model.User;
import cn.ydt.ydt.pojo.user.vo.UserShowInfoVO;
import cn.ydt.ydt.user.service.IUserService;
import cn.ydt.ydt.user.webapi.mapper.UserMapper;
import cn.ydt.ydt.user.webapi.utils.SecurityContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.List;


import static cn.ydt.ydt.common.resful.StatusCode.*;

@DubboService
@Service
@Slf4j
public class UserServiceImpl implements IUserService {

    @Autowired
    UserMapper userMapper;

    //用户注册
    @Override
    public void reg(UserRegisterDTO userRegisterDTO) throws ParseException {
        log.info(userRegisterDTO.toString());
        PasswordUtils passwordUtils = new PasswordUtils();
        String idNumber = userRegisterDTO.getIdNumber();
        String userPhone = userRegisterDTO.getUserPhone();
        String userPassword = userRegisterDTO.getUserPassword();
        String userName = userRegisterDTO.getUserName();
        UserRegisterDTO IdNumberResult = userMapper.findByIdNumber(idNumber);
        UserRegisterDTO userPhoneResult = userMapper.findByUserPhone(userPhone);

        //身份证被注册
        if (IdNumberResult != null && userPhoneResult == null) {
            throw new YdtServiceException(BAD_REQUEST, "身份已存在");
        }
        //电话被注册
        if (IdNumberResult == null && userPhoneResult != null) {
            throw new YdtServiceException(BAD_REQUEST, "电话已存在");
        }
        //身份和电话都被注册
        if (IdNumberResult != null && userPhoneResult != null) {
            throw new YdtServiceException(BAD_REQUEST, "身份和电话已存在");
        }

        userRegisterDTO.setIdNumber(idNumber);
        userRegisterDTO.setUserPhone(userPhone);
        userRegisterDTO.setUserPassword(passwordUtils.encode(userPassword));
        userRegisterDTO.setUserName(userName);
        userRegisterDTO.setBirthday(IDCardsUtils.getBirthday(idNumber));

        userMapper.insert(userRegisterDTO);
    }

    //查看个人信息
    @Override
    public UserShowInfoVO findUserById(Long userId) {
        UserShowInfoVO result = userMapper.findUserById(userId);
        if (result == null) {
            throw new YdtServiceException(BAD_REQUEST, "用户不存在");
        }
        return userMapper.findUserById(userId);
    }

    @Override
    public UserRegisterDTO findByUserPhone(String userPhone) {
        UserRegisterDTO userRegisterDTO = userMapper.findByUserPhone(userPhone);
        return userRegisterDTO;
    }

    @Override
    public void update(UserUpdateDTO userUpdateDTO) throws ParseException {
        Long userId = userUpdateDTO.getUserId();
        String userPhone = userUpdateDTO.getUserPhone();
        String IdNumber = userUpdateDTO.getIdNumber();
        String userEmail = userUpdateDTO.getUserEmail();
        UserShowInfoVO userIdResult = userMapper.findUserById(userId);
        UserRegisterDTO userPhoneResult = userMapper.findByUserPhone(userPhone);
        UserRegisterDTO userIdNumberResult = userMapper.findByIdNumber(IdNumber);
        UserShowInfoVO userEmailResult = userMapper.findByUserEmail(userEmail);
        userUpdateDTO.setBirthday(IDCardsUtils.getBirthday(IdNumber));
        if (userIdResult == null) {
            throw new YdtServiceException(BAD_REQUEST, "用户不存在");
        }
        if (userPhoneResult != null) {
            if (userPhone == userUpdateDTO.getUserPhone()) {
                userMapper.update(userUpdateDTO);
                return;
            }
            throw new YdtServiceException(BAD_REQUEST, "电话已存在");
        }
        if (userIdNumberResult != null) {
            if (IdNumber == userUpdateDTO.getIdNumber()) {
                userMapper.update(userUpdateDTO);
                return;
            } else {
                throw new YdtServiceException(BAD_REQUEST, "身份已经存在");
            }
        }
        if (userEmailResult != null) {
            if (userEmail == userUpdateDTO.getUserEmail()) {
                userMapper.update(userUpdateDTO);
                return;
            }
            throw new YdtServiceException(BAD_REQUEST, "邮件已经存在");
        }
        userMapper.update(userUpdateDTO);
    }

    @Override
    public UserShowInfoVO getUserByUserId() {
        Long userId = SecurityContextUtils.getUserId();
        UserShowInfoVO userShowInfoVO = userMapper.findUserById(userId);
        if (userShowInfoVO == null) {
            throw new YdtServiceException(BAD_REQUEST, "获取到的用户信息为空");
        }
        return userShowInfoVO;
    }

    @Override
    public UserShowInfoVO getUserByUserIdToCommunityId(Long communityId) {
        Long userId = SecurityContextUtils.getUserId();
        UserShowInfoVO userShowInfoVO = userMapper.findUserByIdToCommunityId(userId,communityId);
        if (userShowInfoVO == null){
            throw new YdtServiceException(BAD_REQUEST,"获取到的用户信息为空");
        }
        return userShowInfoVO;
    }

    @Override
    public List<User> selectByIsVolunteer() {
        List<User> users = userMapper.selectByIsVolunteer();
        for (User u : users) {
            String img = u.getUserIcon();
            u.setUserIcon(FileNameUtils.filePath(img));
        }
        return users;
    }

    @Override
    public void isDeleted(Boolean isDeleted, Long userId) {
        userMapper.isDeleted(isDeleted, userId);
    }

    @Override
    public void updateIcon(String userIcon) {
        UserJwtVO userInfo = SecurityContextUtils.getUserInfo();
        Long userId = userInfo.getUserId();
            userMapper.updateIcon(userIcon,userId);
            log.info("输出图片名字{}",userIcon);
    }

    @Override
    public String refillingIcon() {
        Long userId = SecurityContextUtils.getUserId();
        UserShowInfoVO userShowInfoVO = userMapper.findUserById(userId);
        String icon = userShowInfoVO.getUserIcon();
        String img = FileNameUtils.filePath(icon);
        return img;
    }

}

